//
//  ModifyRecvAdrViewController.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "ModifyRecvAdrViewController.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "SetPasswordViewController.h"
#import "UserDataSingleton.h"
#import "CommFunc.h"

@interface ModifyRecvAdrViewController()<QXNetWorkToolDelegate, UITextViewDelegate>


@end


@implementation ModifyRecvAdrViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.nameField.text = self.name;
    self.phoneField.text = self.phone;
    self.codeField.text = self.code;
    self.addressField.text = self.address;
    
    //叉号，一次性删除输入框中的内容
    [self.nameField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.phoneField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.codeField setClearButtonMode:UITextFieldViewModeWhileEditing];
//    [self.addressField setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    //数字键盘
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    self.codeField.keyboardType = UIKeyboardTypeNumberPad;
    
    //编辑时清空
    self.nameField.clearsOnBeginEditing = YES;
    self.phoneField.clearsOnBeginEditing = YES;
    self.codeField.clearsOnBeginEditing = YES;
//    self.addressField.clearsOnBeginEditing = YES;
    
    //placeholder字体、颜色
    [self.nameField setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15]
                  Color:UIColorRGBA(174, 174, 174, 1)];
    
    [self.phoneField setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15] Color:UIColorRGBA(174, 174, 174, 1)];
    
    [self.codeField setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15] Color:UIColorRGBA(174, 174, 174, 1)];
    
//    [self.addressField setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15] Color:UIColorRGBA(174, 174, 174, 1)];
    
    //手势
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self.view addGestureRecognizer:tapGestureRecognizer];
    
    self.placeHolder = @"请填写详细地址";
    
    if ([self.addressField.text isEqualToString:@""])
    {
        self.addressField.text = self.placeHolder;
        self.addressField.textColor = UIColorRGBA(174, 174, 174, 1);
    }
    else
    {
        self.addressField.textColor = UIColorRGBA(0, 0, 0, 1);
    }
    
    self.addressField.delegate = self;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text = @"";
    self.addressField.textColor = UIColorRGBA(0, 0, 0, 1);
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""])
    {
        textView.text = self.placeHolder;
        self.addressField.textColor = UIColorRGBA(174, 174, 174, 1);
    }
}

-(void)tap
{
    [self.nameField resignFirstResponder];
    [self.phoneField resignFirstResponder];
    [self.codeField resignFirstResponder];
    [self.addressField resignFirstResponder];
}

- (BOOL)isCorrectPhone
{
    if ([self.phoneField.text isEqualToString:@""])
    {
        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];
        return NO;
    }
    
    if ([CommFunc isPureInt:self.phoneField.text])
    {
        if(![CommFunc isValidateMobile:self.phoneField.text])
        {
            [MBProgressHUD showError:@"请正确输入手机号" toView:self.view];
            
            return NO;
        }
    }
    else
    {
        [MBProgressHUD showError:@"请正确输入手机号" toView:self.view];
        
        return NO;
    }
    
    return YES;
}

- (IBAction)save:(id)sender
{
    UserDataSingleton *userData = [UserDataSingleton shareInstance];
    if(![userData isLogin])
    {
        [userData pleaseLogin:self];
        return;
    }
    
    if ([self.nameField.text isEqualToString:@""])
    {
        [MBProgressHUD showError:self.nameField.placeholder];
        return;
    }
    
    if ([self.phoneField.text isEqualToString:@""])
    {
        [MBProgressHUD showError:self.phoneField.placeholder];
        return;
    }
    
    if ([self isCorrectPhone] == NO)
    {
        return;
    }
    
    if ([self.codeField.text isEqualToString:@""])
    {
        [MBProgressHUD showError:self.codeField.placeholder];
        return;
    }
    
    if ([self.addressField.text isEqualToString:@""])
    {
        [MBProgressHUD showError:self.placeHolder];
        return;
    }

    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;

    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:ModifyReciveAddressInterface andParameter:
             @[@{@"user_id":[UserDataSingleton shareInstance].user_id,
                 @"address":self.addressField.text,
                 @"consignees_name":self.nameField.text,
                 @"consignees_phone":self.phoneField.text,
                 @"postcode":self.codeField.text
                 }] tag:1];
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"修改收货地址";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
        {
            [MBProgressHUD showSuccess:mes];
            [self.navigationController popViewControllerAnimated:YES];
            break;
        }
    }
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:message];
            break;
    }
}

//-(void)GetSuccessResponse:(id)Response andTag:(int)tag
//{
//    [MBProgressHUD hideHUD];
//    
//    switch (tag)
//    {
//        case 1:
//        {
//            
//            break;
//        }
//    }
//}


@end




















